package com.livecooking;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivity;

public class MainActivity extends SlidingActivity {

	private FoodsDataSource datasource;
	DataBaseHelp dataHelp;
	ListAdapter image;

	String[] foodNameList;
	List<Integer> foodIdList;
	List<Integer> foodBookmarkList;
	List<Integer> listId;
	List<String> listName;
	List<Integer> listBookmark;

	String[] mMenuOption = new String[] { "Tất cả", "Luộc", "Bánh", "Nướng",
			"Kho", "Canh", "Xào", "Rán", "Nộm", "Khác", "BookMark" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setBehindContentView(R.layout.sliding_menu);
		getSlidingMenu().setBehindOffset(70);

		// MainActivity.this.deleteDatabase("LiveCooking.sqlite");

		// Copy DataBase LiveCooking
		getSlidingMenu().showMenu();

		dataHelp = new DataBaseHelp(this);

		try {

			dataHelp.createDataBase();

		} catch (IOException ioe) {

			throw new Error("Unable to create database");

		}

		try {

			dataHelp.openDataBase();

		} catch (SQLException sqle) {

			throw sqle;

		}
		datasource = new FoodsDataSource(this);
		datasource.open();
		// load listview sliding
		ListView listMenuOption = (ListView) findViewById(R.id.listMenu);

		ListMenuAdapter adapterMenuOption = new ListMenuAdapter(this,
				mMenuOption);
		listMenuOption.setAdapter(adapterMenuOption);

		listMenuOption.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				getSlidingMenu().showContent();

				TextView txtTitle = (TextView) findViewById(R.id.txtTitle);
				txtTitle.setText(mMenuOption[position]);
				if (position == 0) {

					List<Food> listFood = datasource.getAllFood();
					listName = new ArrayList<String>();
					listId = new ArrayList<Integer>();
					listBookmark = new ArrayList<Integer>();

					for (int i = 0; i < listFood.size(); i++) {
						listName.add(listFood.get(i).getName());
						listId.add(listFood.get(i).getId());
						listBookmark.add(listFood.get(i).getBookMark());
					}
					foodBookmarkList=listBookmark;
					foodIdList=listId;
					foodNameList = listName.toArray(new String[listName.size()]);
					ListView listView = (ListView) findViewById(R.id.list);
					ListAdapter adapter = new ListAdapter(MainActivity.this,
							foodNameList, foodIdList);
					listView.setAdapter(adapter);
				} else if (position == 10) {
					List<Food> listFood = datasource.getAllBookmark();
					listName = new ArrayList<String>();
					listId = new ArrayList<Integer>();
					listBookmark = new ArrayList<Integer>();

					for (int i = 0; i < listFood.size(); i++) {
						listName.add(listFood.get(i).getName());
						listId.add(listFood.get(i).getId());
						listBookmark.add(listFood.get(i).getBookMark());
					}
					foodBookmarkList=listBookmark;
					foodIdList=listId;
					foodNameList = listName.toArray(new String[listName.size()]);
					ListView listView = (ListView) findViewById(R.id.list);
					ListAdapter adapter = new ListAdapter(MainActivity.this,
							foodNameList, foodIdList);
					listView.setAdapter(adapter);
				} else {
					List<Food> listFood = datasource
							.getFoodType(mMenuOption[position]);
					listName = new ArrayList<String>();
					listId = new ArrayList<Integer>();
					listBookmark = new ArrayList<Integer>();

					for (int i = 0; i < listFood.size(); i++) {
						listName.add(listFood.get(i).getName());
						listId.add(listFood.get(i).getId());
						listBookmark.add(listFood.get(i).getBookMark());
					}
					foodBookmarkList=listBookmark;
					foodIdList=listId;
					foodNameList = listName.toArray(new String[listName.size()]);
					ListView listView = (ListView) findViewById(R.id.list);
					ListAdapter adapter = new ListAdapter(MainActivity.this,
							foodNameList, foodIdList);
					listView.setAdapter(adapter);
				}
			}
		});
		// load listview food
		List<Food> listFood = datasource.getAllFood();
		listName = new ArrayList<String>();
		listId = new ArrayList<Integer>();
		listBookmark = new ArrayList<Integer>();

		for (int i = 0; i < listFood.size(); i++) {
			listName.add(listFood.get(i).getName());
			listId.add(listFood.get(i).getId());
			listBookmark.add(listFood.get(i).getBookMark());
		}
		foodBookmarkList=listBookmark;
		foodIdList=listId;
		foodNameList = listName.toArray(new String[listName.size()]);
		ListView listView = (ListView) findViewById(R.id.list);
		ListAdapter adapter = new ListAdapter(this, foodNameList, foodIdList);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// Integer foodId = listId.get(position);
				String foodName = foodNameList[position];
				// Integer s = listId.get(position);
				String Id = foodIdList.get(position).toString();
				String Bookmark = foodBookmarkList.get(position).toString();
				// String howToMake =
				// datasource.getHowToMake(foodId).toString();
				String howToMake = datasource.getHowToMake(foodName);
				Intent intent = new Intent(MainActivity.this,
						DetailsActivity.class);
				intent.putExtra(DetailsActivity.BOOKMARK, Bookmark);
				intent.putExtra(DetailsActivity.IMV_HOW_TO_MAKE, Id);
				intent.putExtra(DetailsActivity.FOOD_NAME, foodName);
				intent.putExtra(DetailsActivity.HOW_TO_MAKE, howToMake);
				startActivity(intent);
			}
		});
	}

	public void onClickShowSliding(View view) {
		getSlidingMenu().showMenu();
	}

	public void onClickSearch(View view) {

		EditText edtSearch = (EditText) findViewById(R.id.edtSearch);
		String search = edtSearch.getText().toString();
		List<String> foodNames = new ArrayList<String>();
		List<Integer> foodIds = new ArrayList<Integer>();
		List<Integer> foodBookmarks = new ArrayList<Integer>();
		for (int i = 0; i < listName.size(); i++) {
			String foodName = listName.get(i);
			if (foodName.contains(search) == true) {
				foodNames.add(listName.get(i));
				foodIds.add(listId.get(i));
				foodBookmarks.add(listBookmark.get(i));

			}
		}
		
		if (foodNames != null) {
			foodBookmarkList=foodBookmarks;
			foodIdList=foodIds;
			foodNameList = foodNames.toArray(new String[foodNames.size()]);
			ListView listView = (ListView) findViewById(R.id.list);
			ListAdapter adapter = new ListAdapter(MainActivity.this,
					foodNameList, foodIdList);
			listView.setAdapter(adapter);
		}

	}

	@Override
	protected void onResume() {

		super.onResume();
		listBookmark = datasource.getBookmarkList();
	}

}
